package me.zhengjie.org.teambase.service.impl;

import me.zhengjie.org.teambase.domain.TeamBase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.org.teambase.repository.TeamBaseRepository;
import me.zhengjie.org.teambase.service.TeamBaseService;
import me.zhengjie.org.teambase.service.dto.TeamBaseDto;
import me.zhengjie.org.teambase.service.dto.TeamBaseQueryCriteria;
import me.zhengjie.org.teambase.service.mapper.TeamBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "teamBase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TeamBaseServiceImpl implements TeamBaseService {

    private final TeamBaseRepository teamBaseRepository;

    private final TeamBaseMapper teamBaseMapper;

    public TeamBaseServiceImpl(TeamBaseRepository teamBaseRepository, TeamBaseMapper teamBaseMapper) {
        this.teamBaseRepository = teamBaseRepository;
        this.teamBaseMapper = teamBaseMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(TeamBaseQueryCriteria criteria, Pageable pageable){
        Page<TeamBase> page = teamBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(teamBaseMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<TeamBaseDto> queryAll(TeamBaseQueryCriteria criteria){
        return teamBaseMapper.toDto(teamBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public TeamBaseDto findById(Integer id) {
        TeamBase teamBase = teamBaseRepository.findById(id).orElseGet(TeamBase::new);
        ValidationUtil.isNull(teamBase.getId(),"TeamBase","id",id);
        return teamBaseMapper.toDto(teamBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public TeamBaseDto create(TeamBase resources) {
        return teamBaseMapper.toDto(teamBaseRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(TeamBase resources) {
        TeamBase teamBase = teamBaseRepository.findById(resources.getId()).orElseGet(TeamBase::new);
        ValidationUtil.isNull( teamBase.getId(),"TeamBase","id",resources.getId());
        teamBase.copy(resources);
        teamBaseRepository.save(teamBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            teamBaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TeamBaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TeamBaseDto teamBase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("组织名称", teamBase.getTeamName());
            map.put("特殊情况说明", teamBase.getSpecialCase());
            map.put("创建时间", teamBase.getStartTime());
            map.put("撤销时间", teamBase.getEndTime());
            map.put("描述", teamBase.getDescribe());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}